#include "model/face.h"
#include "model/halfedge.h"

Face::Face(Object &object, HalfEdge &edge): object(&object), edge(&edge) {}

const std::vector<const Vertex*> Face::getVertexs() const {
    std::vector<const Vertex*> p;

    HalfEdge *edge = this->edge;

    do {
        p.push_back(edge->begin);
        edge = edge->next;
    } while( this->edge != edge );

    return p;
}

const std::vector<const HalfEdge*> Face::edges() const {
    std::vector<const HalfEdge*> e;

    HalfEdge *edge = this->edge;

    do {
        e.push_back(edge);
        edge = edge->next;
    } while( this->edge != edge );

    return e;
}

const Point Face::getNormalVector() const {
    Vertex p1 = *edge->begin;
    Vertex p2 = *edge->end;
    Vertex p3 = *edge->next->end;

    Point v1 = p2 - p1;
    Point v2 = p3 - p1;

    return v1.vetorial(v2).unit();
}

const Point Face::getCenter() const {
    Point greater = Point::min();
    Point lower = Point::max();

    for ( const Vertex *vertex: getVertexs() ) {
        if ( vertex->x < lower.x )
            lower.x = vertex->x;
        else if ( vertex->x > greater.x )
            greater.x = vertex->x;

        if ( vertex->y < lower.y )
            lower.y = vertex->y;
        else if ( vertex->y > greater.y )
            greater.y = vertex->y;

        if ( vertex->z < lower.z )
            lower.z = vertex->z;
        else if ( vertex->z > greater.z )
            greater.z = vertex->z;
    }

    return (greater+lower)/2;
}

